package dao;

import org.apache.ibatis.annotations.*;
import pojo.po.User;

import java.util.List;
import java.util.Set;

public interface UserMapper {
    @Select("select * from t_user u " +
            "where username like concat ('%',#{user.username},'%') " +
            "limit #{index},#{size}")
    @Results(id="usersAndRoles",value={
    @Result(column = "id",property = "id"),
            @Result(property = "roles",column ="id",many = @Many(select = "dao.RoleMapper.findByUid"))
    })
    List<User> findAllUsers(@Param("user") User user ,@Param("index") Integer index, @Param("size") Integer pageSize);

    @Select("select count(*) from t_user " +
            "where username like concat ('%',#{username},'%')")
    int countUsers(User user);


    //删除user表数据
    int deleteUserById(@Param("id") String id);

    //删除ur中间表的数据
    int deleteUserFromUR(@Param("id") String id);
    //新增ur中间表数据
    int addUserFromUR(@Param("id") String id ,@Param("rids") List<String> rid);

    @Update("update t_user set username = #{username},password = #{password},remark = #{remark},updateTime = #{updateTime, jdbcType=TIMESTAMP},email=#{email} where id = #{id}")
    int updateUser(User user);

    //@Insert("insert into t_user values(null,#{username},#{password},#{remark},#{email},#{createTime, jdbcType=TIMESTAMP},#{updateTime, jdbcType=TIMESTAMP})")
    int addUser(User user);

    @Select("select * from t_user " +
            "where username like concat ('%',#{username},'%') limit #{index},#{size}")
    @Results(id="findUserByLike",value={
            @Result(column = "id",property = "id"),
            @Result(property = "roles",column ="id",many = @Many(select = "dao.RoleMapper.findByUid"))
    })
    List<User> findUserByLike(@Param("username") String name, @Param("index") Integer index, @Param("size") Integer pageSize);


    @Select("<script>" +
            "select count(*) from t_user where username like concat ('%',#{username},'%')</script> ")
    int countUsersByLike(String username);
}
